<?php
	//User interface for viewing completed inspections in list format.
	include 'commonAccountFunctions.php';
	include 'templateHandler.php';
	
	//session_start();
	$user = $_SESSION['user'];
	
	$user = checkAuthentication();
	

	print_header();
?>	
		<div id="topbar">
			<div id="title"> Lab-Check</div>
			<div id="rightbutton"> <a href="index.php" >Log Out</a> </div>
		</div>
		<div id="tributton">
			<div class="links">
				<a href="controlpanel.php">Control Panel</a><a id="pressed" href="completedLists.php">Complete Checklists</a>
			</div>
		</div>

		<div id="content">
			<ul class="pageitem" style="padding:5px">
				<li> <strong>Username:</strong>  <?=$user?> </li>
			</ul>
		
		<?php 
		$viewFile = isset($_REQUEST["viewFile"]);
		$deleteFile = isset($_REQUEST["delete"]);
		$share = isset($_REQUEST["share"]);
		//If the user has selected a file to view.
		if($viewFile){ ?>
			<div class="forms"> <?php
			$arrayOfStrings = readcomplete($_REQUEST["file"] . ".txt");
			$parsedTemplate = parseTemplateLines($arrayOfStrings);
			$fileInfo = $parsedTemplate["fileInfo"];
			loadTemplate($parsedTemplate["groups"], $parsedTemplate["fileInfo"]);
			?></div><?php
		}
		//the user has selected 
		elseif($deleteFile){
			echo "User clicked Delete";
			if(isset($_REQUEST["file"])){
				$fileName = $_REQUEST["file"];
				print_r($fileName);
				foreach($fileName as $file){
					unlink("../files/completedInspections/" . $file . ".txt");
				} ?>
				<script type="text/javascript">
				window.location = "completedLists.php";
				</script>
	<?php	}
			else{ ?>
				<script type="text/javascript">
					window.location = "completedLists.php";
				</script> <?php
				//user did not select a file to delete.
			}
		}
		elseif($share){ //a file was selected to be sent.
			$email=getUserEmail($user); ?>
			<form action="mailFile.php" method="post" name="send">
				<?php 
				$i = 0;
				foreach($_REQUEST["file"] as $name){ 
					$i++;?>
					<input type="hidden" name="fileName" value="../files/completedInspections/<?=$name?>.txt"</input>
				<?php } ?>
				<input type="hidden" name="email" value="<?=$email?>"</input>
			</form>
			
			<script type="text/javascript">
				document.send.submit();
			</script> <?php
		}
		else{ 
			
			$completedList = file("../files/facilityList.txt");   // list of facilities
			
			if(isset($_REQUEST["fac"])){
				$selectedFac = $_REQUEST["fac"];
			} else {
				 // check first if completedList is non-empty ???????
				$selectedFac = $completedList[0];
			}
			
			?>
			<ul class="pageitem">
			<form action="completedLists.php" name="facForm">
			<li style="margin:5px">
				<span class="header" style="text-align:center; font-size:20px">Facility:</span>
			</li>
			<li class="select"><select name="fac" value="file" onchange='changeList()'>
						<?php 
							foreach($completedList as $file){
								if($selectedFac == $file){ ?>
									<option name="room" value="<?=$file?>" selected="true"> <?=$file?> </option>
								<?php } else { ?> <option name="room" value="<?=$file?>"> <?=$file?> </option>
							<?php }
							}?>
								</select>
								<span class="arrow"></span>
			</form>
			</ul>
			<ul class="pageitem">
				
				<script type="text/javascript">
				function changeList(){
					//alert("hello");
					document.facForm.submit();
				}
				</script>
				
				<?php
				//reads in a specified directory that is in the file folder.
					function readFiles($folderName){
						$dir = "../files/" . $folderName;
						$fileList = array();
						if (is_dir($dir)) {
							if ($dh = opendir($dir)) {
								$i = 0;
								while (($file = readdir($dh)) !== false) {
									if($file != "." and $file != ".." and substr($file, -4) == ".txt"){
											$fileList[$i] = substr($file, 0, -4);
											$i++;
									}//print_r($fileList);
								}
								closedir($dh);
							}
						}
						//print_r($fileList);
						return $fileList;
					}
					$completedList = readFiles("completedInspections");
					//print_r($completedList);
					?>
				<li style="margin:5px"><span class="header" style="text-align:center; font-size:20px">Completed Lists:</span></li>
					<form action="completedLists.php" method="post">
						<?php foreach($completedList as $file){
								//print($file);
								//if(isset($_REQUEST["fac"])){
									$fac = trim($selectedFac);
									$fileExp = explode("_", $file);
									$fileFac = $fileExp[0];
									//print($fileFac);
									//print($fac);
									if($fac == trim($fileFac)){ ?>
										<li class="checkbox"><span class="name"> <a href="completedLists.php?file=<?=$file?>&viewFile=view"><?=$file?></a></span>
									<input class="slider" name="file[]" value="<?=$file?>" type="checkbox" /></li>
									<?php $fileFac = null; $fac = null;
									}
								/*}else{		?>
									<li class="checkbox"><span class="name"> <a href="completedLists.php?file=<?=$file?>&viewFile=view"><?=$file?></a></span>
										<input name="file[]" value="<?=$file?>" type="checkbox" /></li>
									<?php
									}*/
								}
							?>
						<li class="button"><input type="submit" name="share" value="Share Selected"/></li>
						<li class="button"><input type="submit" name="delete" value="Delete Selected"/></li>
					</form>
			</ul>
	<?php } 
	?></div><?php	

	print_footer();
?>